//
//  QSVisitItemView.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/5/21.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSVisitItemView.h"
#import "QSHomeHeaderItemCell.h"
#import "QSHomeItemFlowLayout.h"
#import "QSVisitInterFace.h"
@interface QSVisitItemView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) QSHomeItemFlowLayout *layout;
//装有item信息的数组
@property (nonatomic, strong) NSMutableArray *itemArray;
@end
static NSString * const KHomeItemCell = @"KHomeItemCell";

@implementation QSVisitItemView
#pragma mark - **************** Initials
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        self.layer.cornerRadius = 5;
        [self configSubView];
        [self loadData];//加载item数据
    }
    return self;
}

- (void)configSubView{
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.height.equalTo(self);
    }];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _layout = [[QSHomeItemFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.layer.cornerRadius = 5;
        [_collectionView registerClass:[QSHomeHeaderItemCell class] forCellWithReuseIdentifier:KHomeItemCell];
        _collectionView.layer.masksToBounds = YES;
        _collectionView.layer.shadowColor = [UIColor QSColorGrayA1A1A1].CGColor;
        _collectionView.layer.shadowOffset = CGSizeMake(0, 0);
        _collectionView.layer.shadowOpacity = 0.2f;
        _collectionView.layer.shadowRadius = 10;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}
- (void)loadData{
    [QSVisitInterFace getVisitHeaderItemAndBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSUserHomeItemModel * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            [self checkItemCountWithArray:result.iconList];
            [self.collectionView reloadData];
        }
    }];
}

#pragma mark - CollectionViewDelegate && DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QSItemModel *model = self.itemArray[indexPath.item];
    QSHomeHeaderItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHomeItemCell forIndexPath:indexPath];
    cell.isVisitCell = YES;
    cell.visitItemModel = model;
    return cell;
}

- (void)checkItemCountWithArray:(NSArray *)itemArr{
    self.itemArray = itemArr.mutableCopy;
    NSLog(@"走访item数组：%@", self.itemArray);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    QSItemModel *model = self.itemArray[indexPath.item];
    if (self.delegate && [self.delegate respondsToSelector:@selector(visitItemView:navigationCollectionViewdidSelectItemAtIndexPath:itemModel:)]) {
        [self.delegate visitItemView:self navigationCollectionViewdidSelectItemAtIndexPath:indexPath itemModel:model];
    }
}


@end
